/*******************************************************************************
 ** Name: cmd.cpp                                                             **
 ** Description:                                                              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved 2-clause BSD License                **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  Redistribution and use in source and binary forms, with or without       **
 **  modification, are permitted provided that the following conditions are   **
 **  met:                                                                     **
 **                                                                           **
 **  * Redistributions of source code must retain the above copyright notice, **
 **    this list of conditions and the following disclaimer.                  **
 **  * Redistributions in binary form must reproduce the above copyright      **
 **    notice, this list of conditions and the following disclaimer in the    **
 **    documentation and/or other materials provided with the distribution.   **
 **                                                                           **
 **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
 **                                                                           **
 ******************************************************************************/
// Modified $Date: 2010-03-16 22:12:41 -0600 (Tue, 16 Mar 2010) $ by $Author: bjames $
#define db_cpp_ID    "$Id: db.cpp 131 2010-03-17 04:12:41Z bjames $"

#include <string>
#include <vector>

#include "container.hpp"
#include "db.hpp"
#include "sanctiond.hpp"
#include "log.hpp"

const char *
AreaDB::qAreas = "SELECT * FROM areas WHERE setting = %i";

const char *
AreaDB::qSettings = "";

const char *
AreaDB::qNPCs = "";

///////////////////////////////[ DBRef
DBRef::DBRef(std::string q,std::string p)
: MySQLDB("gsdata")
{
	SetHost("localhost");
	SetUser("gsdbreader");
	SetPass(p);
	query = q;
}

mysqlpp::StoreQueryResult
DBRef::ExecuteQuery()
{
	Init();
	return Query(query.data());
}


///////////////////////////////[ AreaDB
AreaDB::AreaDB(std::string f)
: MySQLDB(f)
{

}

std::vector<MySQLArea>
AreaDB::LoadAreas(int setting)
{
    DBGFUNC();

    std::vector<MySQLArea>   v;
    if (bInitialized == false) return v;

    char buff[256];
    std::string     qry(qAreas);
    sprintf(buff,qry.c_str(),setting);

    mysqlpp::Query query = conn.query(buff);
    query.storein(v);

    return v;
}

int
AreaDB::LoadNPCs()
{
    DBGFUNC();

    return 0;
}
///////////////////////////////[ MySQLDB
/// We only load help tutorial and Agency HQ in memory at the moment. All
/// other areas are pulled from the DB as they are visited.
//mysqlpp::StoreQueryResult
mysqlpp::StoreQueryResult
MySQLDB::Query(const char * q)
{
    DBGFUNC();
    mysqlpp::Query query  = conn.query(q);
    mysqlpp::StoreQueryResult res = query.store();

    return res;
}

bool
MySQLDB::Init()
{
    DBGFUNC();

    debug::log->out(LOG_DEBUG,"mysql++.connect(%s,%s,%s,%s)",
             db.c_str(),host.c_str(),dbuser.c_str(),dbpass.c_str());
    if (conn.connect(db.c_str(),host.c_str(),dbuser.c_str(),dbpass.c_str()))
    {
        bInitialized = true;
        return true;
    }

    return false;
}

MySQLDB::MySQLDB(std::string s)
    : DataBase(s)
{
    
}

///////////////////////////////[ ServerCfg
int
ServerCfg::Parse()
{
    DBGFUNC();

    char buff[256];
    char a[64], v[64];
    //std::string buff;

    while (!conf.eof()) {
        conf.getline(buff,256);
        if (buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' || buff[0] == '\0')
            continue;

        sscanf(buff,"%s = %s",a,v);
        debug::log->out(LOG_DEBUG,"Adding %s = %s",a,v);
        avpairs[a] = v;
    }

    return avpairs.size();
}

bool
ServerCfg::Init()
{
    DBGFUNC();

    conf.open(db);
    if (conf.good()) {
        bInitialized = true;
        return true;
    }

    return false;
}

ServerCfg::ServerCfg(std::string s)
    : DataBase(s)
{
    debug::log->out(LOG_DEBUG,"Using conf: %s",s.c_str());
}

///////////////////////////////[ DataBase

//constructor
DataBase::DataBase(std::string s)
{
    db = s;
    bInitialized = false;
}

//destructor
DataBase::~DataBase(void)
{
}
